﻿/// ****************************************************************************
/// Author:	        Robin Zhu
/// Create Date:	2015-11-25
/// Purpose: 		常用正则表达式工具类
/// ****************************************************************************
/// Modify By		Date			Remark
/// ****************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RB.Utility
{
    public class RegexHelper
    {
        public const string IPAddressPattern = @"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$";

        public static bool IsIPAddress(string input)
        {
            return Regex.IsMatch(input, IPAddressPattern);
        }

        public const string EmailAddressPattern = @"^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$";

        public static bool IsEmailAddress(string input)
        {
            return Regex.IsMatch(input, EmailAddressPattern);
        }

        public const string IPSubnetPattern = @"^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/(\d{1,2})$";

        public static bool ExtractIPSubnet(string input, out string ipAddress, out string maskbits)
        {
            var match = Regex.Match(input, IPSubnetPattern);
            if (match.Success)
            {
                ipAddress = match.Groups[1].Value;
                maskbits = match.Groups[2].Value;
                return true;
            }
            ipAddress = null;
            maskbits = null;
            return false;
        }
    }
}
